﻿using System;
using System.Data;
using System.Windows.Forms;
using DBConnect;

namespace TISS_Add_in
{
    public partial class uc_Login : UserControl
    {
        private uc_ModConfig ucModConf = null;
        
        public uc_Login()
        {
            InitializeComponent();
            txtLogin.Focus();
        }

        public uc_Login(uc_ModConfig uc)
        {
            InitializeComponent();
            txtLogin.Focus();
            this.ucModConf = uc;
        }

        private void limpaCampos()
        {
            txtLogin.Text = "";
            txtSenha.Text = "";
        }

        private void btnLogar_Click(object sender, EventArgs e)
        {
            if (txtLogin.Text == "")
                MessageBox.Show(btnLogar, " Login obrigatório ");
            else if (txtSenha.Text == "")
                MessageBox.Show(btnLogar, " Senha obrigatória ");
            else
            {
                string senhaHash = AddinUtils.CalculateMD5Hash(txtSenha.Text);
                DataTable dt = CommandModConf.verificaLogin(null, txtLogin.Text, senhaHash);                
                if (dt.Rows.Count == 0)
                
                {
                    MessageBox.Show(btnLogar, "Login ou senha inválidos!");
                    limpaCampos();
                }
                else
                {
                    if (ucModConf == null)
                    {
                        Globals.Ribbons.Ribbon1.grpAcoes.Visible = true;
                        Globals.Ribbons.Ribbon1.grpProcedimentos.Visible = true;
                        Globals.Ribbons.Ribbon1.btnAcompanhar.Enabled = true;
                        Globals.Ribbons.Ribbon1.btnDemonstrativos.Enabled = true;

                        Globals.ThisAddIn.UsuarioID = Convert.ToInt64(dt.Rows[0]["ID"].ToString());
                        Globals.ThisAddIn.PrestadorNome = dt.Rows[0]["nome"].ToString();

                        Globals.Ribbons.Ribbon1.btnAcompanhar.Checked = true;
                        Globals.ThisAddIn.AcompanhaGuiaCTP = Globals.ThisAddIn.CustomTaskPanes.Add(new uc_AcompanhaGuia(), "Lotes");
                        Globals.ThisAddIn.AcompanhaGuiaCTP.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionLeft;
                        Globals.ThisAddIn.AcompanhaGuiaCTP.Width = 315;
                        Globals.ThisAddIn.AcompanhaGuiaCTP.Visible = true;
                    }
                    else
                    {
                        ucModConf.UsuarioID = Convert.ToInt64(dt.Rows[0]["ID"].ToString());
                        ucModConf.NomeUsuario = dt.Rows[0]["nome"].ToString();
                        ucModConf.lblMensagem.Text = "Usuário autenticado: " + dt.Rows[0]["nome"].ToString();

                        ucModConf.gpbConfOperadora.Enabled = true;
                        ucModConf.btnAddOperadora.Enabled = true;
                        ucModConf.btnRemoveOp.Enabled = true;
                        ucModConf.carregaOperadorasFiliadas();
                    }

                    Globals.ThisAddIn.CustomTaskPanes.Remove(Globals.ThisAddIn.LoginCTP);
                }
            }
        }
    }
}
